#
# Web server configuration module
#
# make a copy of this file and customize that for your work
#
#
# attributes that must always be present are capitalized
#
import atlas, sys
import functools

#
# displayed on the index page
#
TITLE = "Genetrack Browser"

#
# directory setup, a string containing the 
# full path to a directory 
#
HOME = atlas.ENV.HOME_DIR # default location

#
# typically these directories do not need to be altered
#
TEMPLATE_DIR = HOME + "/html"
SESSION_DIR  = HOME + "/html/session"
STATIC_DIR   = HOME + "/html/static"
DOWNLOAD_DIR = HOME + "/html/static/download"
IMAGE_DIR    = HOME + "/html/static/img"
STATIC_URL   = "/static"

#
# database setup
#
organism = "human"
HDF_DATABASE = "%s/db/%s-data.hdf" % (HOME, organism)
SQL_URI      = "sqlite:///%s/db/%s-data.sqlite" % (HOME, organism)

#
# the value of the CHROM_FIELDS could be hardcoded as a list of tuples
# but it is more reliable if we dynamically generate it from a HDF file
#
try:
    from atlas import hdf
    db = hdf.hdf_open( HDF_DATABASE, mode='r' )
    data = hdf.GroupData(db=db, name='H2AZ')
    chromosomes = data.labels
    db.close()
except Exception, exc:
    atlas.error("unable to read chromosomes %s" % exc )
    sys.exit()
    
#
# used to generate the chromosome dropdown menu
#
CHROM_FIELDS = [ (x, x) for x in chromosomes ]

#
# used to generate the zoom dropdown menu
#
LEVELS      = [ 50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000, 50000, 100000, 200000 ]
LEVELS      = map(str, LEVELS)
humanized   = map(atlas.commify, LEVELS)
ZOOM_LEVELS = [ (x, y) for x,y in zip(LEVELS, humanized) ]

#
# generates the dropdown menu for the plot
# first element is the key the second the visible menu item
# keys must map to the PLOT_MAPPER
#
PLOT_CHOICES = [ 
    ('composite', 'Composite'), ('twostrand', 'Two Stranded'),
]

#
# the main plotbuilder
#
from mod454.trackbuilder import build_tracks

#
# customizing the plotbuilder via partial application
#
# for twostrand tracks
#
h2az_twostrand_tracks = functools.partial ( build_tracks, 
        data_label = 'H2AZ', 
        fit_label  = 'H2AZ-SIGMA-20', 
        pred_label = 'PRED-H2AZ-SIGMA-20', 
        strand     = 'twostrand' )

#
# composite tracks
#
h2az_composite_tracks = functools.partial ( build_tracks, 
        data_label = 'H2AZ', 
        fit_label  = 'H2AZ-SIGMA-20', 
        pred_label = 'PRED-H2AZ-SIGMA-20', 
        strand     = 'composite' )

#
# the plotmapper connects the plot choice to a template and a track layout
#
PLOT_MAPPER = {
    'composite': ( 'index.html', h2az_composite_tracks ),
    'twostrand': ( 'index.html', h2az_twostrand_tracks ),
}

#
# a sanity check to catch errors early on
#
for key, value in PLOT_CHOICES:
    assert key in PLOT_MAPPER, "Key '%s' not found in PLOT_MAPPER" % key

#
# socket related configuration
#
SOCKET_PORT = 8080
SOCKET_HOST = "localhost"
THREAD_POOL = 10
